﻿// LogViewController.cpp
#include "LogViewController.h"

#include <algorithm>
#include <mutex>
#include <regex>
#include <string>
#include <vector>

#include "logger\PatternFormatter.h"

namespace Logging {
class LogViewController::Impl {
 public:
  Impl() {
    // 设置一个默认的、适合GUI显示的格式
    const char* default_pattern = "%H:%M:%S.%e [%p] [%c] [%t] %v";
    formatter_ = std::make_unique<PatternFormatter>(default_pattern);
  }

  void AddLog(LogMessageInfo log) {
    log.message = formatter_->format(log);
    std::lock_guard<std::mutex> lock(logs_mutex_);
    all_logs_.push_back(std::move(log));
  }

  void Clear() {
    std::lock_guard<std::mutex> lock(logs_mutex_);
    all_logs_.clear();
  }

  std::vector<LogMessageInfo> GetFilteredLogs(
      const FilterState& filters) const {
    std::vector<LogMessageInfo> filtered_results;
    std::regex search_regex;
    bool is_regex_valid = true;

    if (filters.use_regex && !filters.search_term.empty()) {
      try {
        search_regex =
            std::regex(filters.search_term, std::regex_constants::icase);
      } catch (const std::regex_error&) {
        is_regex_valid = false;
      }
    }

    std::string lower_search_term = filters.search_term;
    if (!filters.use_regex) {
      std::transform(lower_search_term.begin(), lower_search_term.end(),
                     lower_search_term.begin(), ::tolower);
    }

    std::lock_guard<std::mutex> lock(logs_mutex_);
    for (const auto& entry : all_logs_) {
      // 1. Filter by level
      if (filters.enabled_levels.find(entry.level) ==
          filters.enabled_levels.end()) {
        continue;
      }

      // 2. Filter by search term
      if (!filters.search_term.empty()) {
        bool match = false;
        if (filters.use_regex) {
          if (is_regex_valid) {
            match = std::regex_search(entry.message, search_regex);
          }
        } else {
          std::string lower_msg = entry.message;
          std::transform(lower_msg.begin(), lower_msg.end(), lower_msg.begin(),
                         ::tolower);
          if (lower_msg.find(lower_search_term) != std::string::npos) {
            match = true;
          }
        }
        if (!match) {
          continue;
        }
      }

      filtered_results.push_back(entry);
    }
    return filtered_results;
  }

 public:
  mutable std::mutex logs_mutex_;
  std::vector<LogMessageInfo> all_logs_;
  std::unique_ptr<PatternFormatter> formatter_;
};

// --- Public Interface Implementation ---
LogViewController::LogViewController() : pimpl_(std::make_unique<Impl>()) {}
LogViewController::~LogViewController() = default;

void LogViewController::AddLog(LogMessageInfo log) {
  pimpl_->AddLog(std::move(log));
}
void LogViewController::Clear() { pimpl_->Clear(); }

void LogViewController::SetPattern(const std::string& pattern) {
  pimpl_->formatter_ = std::make_unique<PatternFormatter>(pattern);
  // 可选：清除现有日志以应用新格式
  // Clear();
}

std::vector<LogMessageInfo> LogViewController::GetFilteredLogs(
    const FilterState& filters) const {
  return pimpl_->GetFilteredLogs(filters);
}
}  // namespace Logging